﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QLKS.Templates;
using System.Data;
using Infragistics.Windows.Editors;

namespace QLKS.Views
{
    /// <summary>
    /// Interaction logic for frmThemLoaiPhong.xaml
    /// </summary>
    public partial class frmThemLoaiPhong : FormTrans
    {
        public bool UpdateSucces = false;
        Command cm;

        public bool IsEditMode
        {
            get { return (bool)GetValue(IsEditModeProperty); }
            set { SetValue(IsEditModeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsEditMode.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsEditModeProperty =
            DependencyProperty.Register("IsEditMode", typeof(bool), typeof(frmThemLoaiPhong), new UIPropertyMetadata(true));


        public DataTable Data
        {
            get { return (DataTable)GetValue(DataProperty); }
            set { SetValue(DataProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Data.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DataProperty =
            DependencyProperty.Register("Data", typeof(DataTable), typeof(frmThemLoaiPhong), new UIPropertyMetadata(null));



        public frmThemLoaiPhong()
        {
            InitializeComponent();
        }

        public frmThemLoaiPhong(DataTable tb, Command _cm)
        {
            InitializeComponent();
            
            IsEditMode = _cm == Command.View ? false : true;
            cm = _cm;
            if (tb == null)
                Data = ViewModels.LoaiPhongViewModel.CreateNewLoaiPhong();
            else
                Data = tb.Copy();
            if (cm == Command.Copy)
                Data.Rows[0]["malp"] = -1;
            gridMain.DataContext = Data.DefaultView;
            
        }

        private bool KiemTra()
        {
            if (string.IsNullOrEmpty(txtTen.Text))
            {
                MessageBox.Show(M_LAN.Equals("V")?"Chưa nhập loại phòng":"please input room type", "QLKS", MessageBoxButton.OK, MessageBoxImage.Error);
                txtTen.Focus();
                return false;
            }
            else
                if (txtGiaTheoGio.Value == null || (double)txtGiaTheoGio.Value == 0)
                {
                    MessageBox.Show(M_LAN.Equals("V")?"Chưa vào giá theo giờ":"please input price by hour", "QLKS", MessageBoxButton.OK, MessageBoxImage.Error);
                    txtGiaTheoGio.Focus();
                    return false;
                }
                else
                    if (txtGiaTheoNgay.Value == null || (double)txtGiaTheoNgay.Value == 0)
                    {
                        MessageBox.Show(M_LAN.Equals("V")?"Chưa vào giá theo ngày":"please input price by date", "QLKS", MessageBoxButton.OK, MessageBoxImage.Error);
                        txtGiaTheoNgay.Focus();
                        return false;
                    }

            return true;
        }

        //Save
        void tbMain_Cm_Save()
        {
            //Thêm vào CSDL dữ liệu
            this.UpdateLayout();
            if (KiemTra())
            {
                int id = ViewModels.LoaiPhongViewModel.ThemLoaiPhong(Data);
                if (id>=0)
                {
                    Data.Rows[0]["malp"] = id>0?id:(id==0?Data.Rows[0]["malp"]:-1);
                    this.UpdateSucces = true;
                    if (cm == Command.Edit)
                        MessageBox.Show(M_LAN.Equals("V")?"Cập nhật loại phòng thành công!":"Update room type successful", "HotelManager", MessageBoxButton.OK, MessageBoxImage.Information);
                    else
                    {
                        MessageBox.Show(M_LAN.Equals("V")?"Thêm loại phòng thành công!":"insert room type successful", "HotelManager", MessageBoxButton.OK, MessageBoxImage.Information);
                        cm = Command.Edit;                        
                    }
                }
            }
        }

        //Thoát
        void tbMain_Cm_Exit()
        {
            this.Close();
        }

        void tbMain_Cm_New()
        {
            Data = ViewModels.LoaiPhongViewModel.CreateNewLoaiPhong();
            gridMain.DataContext = Data.DefaultView;
            cm = Command.New;
            txtTen.Focus();
        }

        private void FormTrans_Loaded(object sender, RoutedEventArgs e)
        {
            DataTable tb = ViewModels.TienTeViewModel.GetDmTienTe();
            txtTen.Focus();
            if (IsEditMode)
            {
                tbMain.Cm_Save += new ucXToolBar.Execute(tbMain_Cm_Save);
                tbMain.Cm_New+=new ucXToolBar.Execute(tbMain_Cm_New);
            }
            tbMain.Cm_Exit += new ucXToolBar.Execute(tbMain_Cm_Exit);
        }

        private void txtGiaTheoGio_GotFocus(object sender, RoutedEventArgs e)
        {
            (sender as XamNumericEditor).SelectAll();
        }

        private void txtTen_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            Data.Rows[0]["tenlp"] = txtTen.Text;
        }
    }
}
